import { useReducer } from "react";

function App() {
  function reducer(state, action) {
    switch (action.type) {
      case "increment":
        return state + 1;
      case "decrement":
        return state - 1;
      default:
        return state;
    }
  }
  // useReducer第一个参数为reducer函数，第二个则为状态的初始值，
  // 返回的数组第一个参数则为储存的状态，第二个则为触发action的dispatch方法
  const [count, dispatch] = useReducer(reducer, 0);
  return (
    <div>
      <button onClick={() => dispatch({ type: "decrement" })}>-1</button>
      <span>{count}</span>
      <button onClick={() => dispatch({ type: "increment" })}>+1</button>
    </div>
  );
}
export default App;

